package com.sky.controller.admin;

import com.sky.constant.MessageConstant;
import com.sky.result.Result;
import lombok.extern.slf4j.Slf4j;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
import org.springframework.web.multipart.MultipartFile;

import java.io.File;
import java.io.IOException;
import java.util.UUID;

/**
* ClassName: CommonController
* Package: com.sky.controller.admin
* Description:
* @Autehor 屈子岩
* @Create 2024/9/9 17:07
* @Version 1.0
*/
@RestController
@RequestMapping("admin/common")
@Slf4j
public class CommonController {
    @PostMapping("/upload")
    public Result upload(MultipartFile file) {
//        log.info("文件上传:{}", file);
//        String originalFilename = file.getOriginalFilename();
//        try {
//            if (originalFilename != null) {
//                // 利用UUID构造新的文件名称
//                String objectName = UUID.randomUUID().toString() + originalFilename;
//                // 文件的请求路径
//                String filePath = "D:\\IDEA\\Takeaway\\sky-take-out\\img" + objectName;
//                String returnImagePate = "http://127.0.0.1:8080/files/" + objectName;
//                file.transferTo(new File(filePath));
//                return Result.success(returnImagePate);
//            } else {
//                throw new IOException(MessageConstant.UPLOAD_FAILED);
//            }
//        } catch (IOException e) {
//            e.printStackTrace();
//            log.error("文件上传失败:{}", e);
//        }
//        return Result.error(MessageConstant.UPLOAD_FAILED);
        UUID uuid = UUID.randomUUID();
        return Result.success(uuid);
    }
}
